import 'package:json_annotation/json_annotation.dart';
part 'flying_data.g.dart';

@JsonSerializable()
class FlyingData {
  /// 本地消息类型
  @JsonKey(name: 'type')
  @FlyingTypeConverter() // 使用注解方式
  FlyingType? type;

  /// 赠送人头像
  String? senderHeader;

  /// 赠送人名字
  String? senderName;

  /// 接收者人名字
  List<String>? receivers;

  /// 礼物图片地址
  String? giftUrl;

  FlyingData();
  factory FlyingData.fromJson(Map<String, dynamic> json) =>
      _$FlyingDataFromJson(json);
  Map<String, dynamic> toJson() => _$FlyingDataToJson(this);
}

enum FlyingType {
  gift(1, '礼物飘屏'),

  flyingEnd(10086, '自定义消息结束');

  final int value;
  final String name;

  const FlyingType(this.value, this.name);

  // 值到枚举的映射表
  static final Map<int, FlyingType> _byValue = {
    for (final e in FlyingType.values) e.value: e,
  };

  // 通过整数值获取枚举
  static FlyingType? fromValue(int value) => _byValue[value];
}

// 自定义 JsonConverter
class FlyingTypeConverter extends JsonConverter<FlyingType?, int?> {
  const FlyingTypeConverter();

  @override
  FlyingType? fromJson(int? json) {
    if (json == null) {
      return null;
    }
    return FlyingType.fromValue(json);
  }

  @override
  int? toJson(FlyingType? object) {
    return object?.value;
  }
}
